'use strict';

const Utils = require('../utils/Utils');
const BaseService = require('../../libs/base/BaseService');
const PlayerManager = require('./manager/PlayerManager');
const RoomManager = require('./manager/RoomManager');
const path = require('path');
const fs = require('fs');

/**
 * GlobalHall 服务，负责hall的管理进程。
 */
class Globalhall extends BaseService {
  /**
   * 构造函数
   */
  constructor() {
    super('Globalhall');
    this.playerManager = new PlayerManager();
    this.roomManager = new RoomManager();
    console.log(`[Globalhall] 构造函数 isDebug=${this.isDebug}`);
  }
  /**
   * 初始化服务
   */
  async init() {
    console.log(`[Globalhall] 服务初始化完成`);
    const serverConfigPath = path.resolve(process.cwd(), 'config/server.json');
    const serverConfigContent = fs.readFileSync(serverConfigPath, 'utf8');
    this.serverConfig = JSON.parse(serverConfigContent).processes;
    await this.playerManager.init(this.name, this.serverConfig);
    await this.roomManager.init(this.name, this.serverConfig);
  }

  /**
   * 老用户上线，只做记录的作用，返回hall port
   */
  playerLogin(info) {
    return this.playerManager.playerLogin(info);
  }
  /**
   * 新用户注册，生成pid和分配hall进行注册
   */
  async playerRegedit(info) {
    return await this.playerManager.playerRegedit(info);
  }
}

module.exports = Globalhall; 